package com.github.prontera.service;


import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.github.prontera.Shift;
import com.github.prontera.StatusCode;
import com.github.prontera.domain.Client;
import com.github.prontera.domain.ClientRent;
import com.github.prontera.domain.enums.ClientStatusEnum;
import com.github.prontera.domain.enums.YesOrNoEnum;
import com.github.prontera.model.request.RentRequest;
import com.github.prontera.persistence.ClientMapper;
import com.github.prontera.persistence.ClientRentMapper;
import com.github.prontera.persistence.CrudMapper;
import com.github.prontera.util.Page;

/**
 * 达人出租
 */
@Service
public class ClientRentService extends CrudServiceImpl<ClientRent>{
    
	@Autowired
	private ClientMapper clientMapper;
	@Autowired
	private ClientRentMapper clientRentMapper;
	
  	@Autowired
    public ClientRentService(CrudMapper<ClientRent> mapper) {
        super(mapper);
    }

  	@Transactional(propagation = Propagation.REQUIRED)
  	public void insert(RentRequest clientRents,Long clientId) {
  		Client client=new Client();
  		client.setId(clientId);
  		client.setBwhBust(clientRents.getBwhBust());
  		client.setBwhHips(clientRents.getBwhHips());
  		client.setBwhWaist(clientRents.getBwhWaist());
  		client.setHeight(clientRents.getHeight());
  		client.setBirthday(clientRents.getBirthday());
  		client.setLeaseTime(clientRents.getLeaseTime());
  		client.setWechat(clientRents.getWechat());
  		clientMapper.update(client);
  		if(clientRents.getRents()!=null&&clientRents.getRents().size()>0) {
			clientRents.getRents().forEach(e -> {
				e.setClientId(clientId);
				e.setAuditStatus(1);
				if(e.getDeleted()!=null&&YesOrNoEnum.YES==e.getDeleted()){
					e.setStatus(2);
					if(super.update(e)<=0){
						Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "数据异常");
					}
				}else{
					e.setStatus(1);
					if(e.getId()==null){
						if(super.insert(e)<=0){
							Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "数据异常");
						}
					}else{
						if(super.update(e)<=0){
							Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "数据异常");
						}
					}
				}
			});
		}
  		
  		ClientRent clientRent=new ClientRent();
  		clientRent.setClientId(clientId);
  		clientRent.setStatus(1);
  		if(clientRentMapper.count(clientRent)>0){
  			Client client2=new Client();
  			client2.setId(clientId);
  			client2.setIsRent(1);
  			client2.setTalentStatus(ClientStatusEnum.YES);
  			clientMapper.update(client2);
  		}else{
  			Client client2=new Client();
  			client2.setId(clientId);
  			client2.setIsRent(2);
  			client2.setTalentStatus(ClientStatusEnum.NO);
  			clientMapper.update(client2);
  		}
	}
  	
  	public Page<HashMap<String, Object>>  pageList(ClientRent clientRent) {
  		clientRent.setOrderBy(" id  desc");
		long count = clientRentMapper.queryListCount(clientRent);
		Page<HashMap<String, Object>> page = new Page<HashMap<String, Object>>(clientRent.getPageIndex(),clientRent.getPageSize());
		List<HashMap<String, Object>> data =clientRentMapper.queryList(clientRent);
		page.setData(data);
		page.setTotalCount(count);
		return page;
	}

}
